package com.anyonavinfo.cpaddemo.view.settingsview;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.anyonavinfo.cpaddemo.MainActivity;
import com.anyonavinfo.cpaddemo.R;
import com.anyonavinfo.cpaddemo.utils.BrightnessUtils;
import com.anyonavinfo.cpaddemo.utils.killprocessutils.KillProcessUtils;
import com.anyonavinfo.cpaddemo.view.settingsview.wifipw.WifiPwSettingView;

/**
 * Created by ZuiC on 2017/2/5.
 */

public class SettingsView extends RelativeLayout{

    private RecyclerView rvSettingsList;
    private ImageView ivExit;
    private Context mContext;
    private SettingListAdapter settingListAdapter;
    private WifiPwSettingView wifiPwSettingView;
    private boolean isNightMode;
    private KillProcessUtils mKillProcess;
    private ProgressDialog killProcessProgressDialog;
    private LinearLayout lvAboutInfo;

    public SettingsView(Context context) {
        super(context);
        initView(context);
    }

    public SettingsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SettingsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        this.mContext = context;
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.view_settingsview_settingsview, this);

        rvSettingsList = (RecyclerView) view.findViewById(R.id.rv_settingslist);
        wifiPwSettingView = (WifiPwSettingView) view.findViewById(R.id.rv_wifipwsettingview);

        rvSettingsList.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
        rvSettingsList.setLayoutManager(layoutManager);
        settingListAdapter = new SettingListAdapter();
        rvSettingsList.setAdapter(settingListAdapter);

        mKillProcess = new KillProcessUtils(mContext);

        ivExit = (ImageView) view.findViewById(R.id.img_exit);
        lvAboutInfo = (LinearLayout) view.findViewById(R.id.lv_aboutinfo);


        setListener();
    }

    private void setListener(){
        settingListAdapter.setOnRecyclerViewListener(new SettingListAdapter.OnRecyclerViewListener() {
            @Override
            public void onItemClick(final int position) {
                switch (position){
                    case 0:
                        Intent wifiSettingIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        wifiSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(wifiSettingIntent);
                        break;
                    case 1:
                        lvAboutInfo.setVisibility(GONE);
                        wifiPwSettingView.setVisibility(VISIBLE);
                        break;
                    case 2:
                        Intent bluetoothSettingIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
                        bluetoothSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(bluetoothSettingIntent);
                        break;
                    case 3:
                        Intent timeSettingIntent = new Intent(Settings.ACTION_DATE_SETTINGS);
                        timeSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(timeSettingIntent);
                        break;
                    case 5:
                        Intent homeIntent = new Intent("android.settings.HOME_SETTINGS");
                        homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(homeIntent);
                        break;
                    case 6:
                        if (!isNightMode){
                            BrightnessUtils.saveBrightness(mContext, 85);
                            isNightMode = true;
                            settingListAdapter.setSettingItemInfo(6, "日间模式", R.drawable.icon_settings_nightmode);
                            settingListAdapter.notifyDataSetChanged();
                        }else {
                            BrightnessUtils.saveBrightness(mContext, 255);
                            isNightMode = false;
                            settingListAdapter.setSettingItemInfo(6, "夜间模式", R.drawable.icon_settings_daymode);
                            settingListAdapter.notifyDataSetChanged();
                        }
                        break;
                    case 7:
                        killProcessProgressDialog = ProgressDialog.show(mContext, "开始清理内存...",
                                "请稍后...", true, true);
                        killProcessProgressDialog.setCanceledOnTouchOutside(false);
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                mKillProcess.killProcess();
                                killProcessProgressDialog.cancel();
                            }
                        }, 1000);
                        break;
                    case 8:
                        wifiPwSettingView.setVisibility(GONE);
                        lvAboutInfo.setVisibility(VISIBLE);
                        break;
                    default:
                        break;
                }
            }
        });

        ivExit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity)mContext).hideSettingView();
            }
        });
    }
}
